{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "060e8594",
   "metadata": {},
   "source": [
    "## 记录变量数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "50714bcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:Look before you leap!\n"
     ]
    }
   ],
   "source": [
    "import logging\n",
    "logging.warning('%s before you %s', 'Look', 'leap!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c70c686",
   "metadata": {},
   "source": [
    "如你所见，将可变数据合并到事件描述消息中使用旧的 %-s形式的字符串格式化。 这是为了向后兼容：logging 包的出现时间早于较新的格式化选项例如 str.format() 和 string.Template。 这些较新格式化选项 是 受支持的，但探索它们超出了本教程的范围：有关详细信息，请参阅 生效于整个应用程序的格式化样式。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bae40b15",
   "metadata": {},
   "source": [
    "## 更改显示消息的格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "15197ef6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "DEBUG:This message should appear on the console\n",
      "INFO:So should this\n",
      "WARNING:And this, too\n"
     ]
    }
   ],
   "source": [
    "# 要更改用于显示消息的格式，你需要指定要使用的格式:\n",
    "import logging\n",
    "logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)\n",
    "logging.debug('This message should appear on the console')\n",
    "logging.info('So should this')\n",
    "logging.warning('And this, too')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f31c4e9",
   "metadata": {},
   "source": [
    "注意在前面例子中出现的 “root” 已消失。文档 LogRecord 属性 列出了可在格式字符串中出现的所有内容，但在简单的使用场景中，你只需要 levelname （严重性）、message （事件描述，包含可变的数据）或许再加上事件发生的时间。 这将在下一节中介绍。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86cfa475",
   "metadata": {},
   "source": [
    "## 在消息中显示日期/时间"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4c5e9a7",
   "metadata": {},
   "source": [
    "要显示事件的日期和时间，你可以在格式字符串中放置 '%(asctime)s'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "77f0b33d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-06-02 16:29:42,288 is when this event was logged.\n"
     ]
    }
   ],
   "source": [
    "import logging\n",
    "\n",
    "logging.basicConfig(format=\"%(asctime)s %(message)s\")\n",
    "logging.warning(\"is when this event was logged.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7b8ccea",
   "metadata": {},
   "source": [
    "日期/时间显示的默认格式（如上所示）类似于 ISO8601 或 RFC 3339 。 如果你需要更多地控制日期/时间的格式，请为 basicConfig 提供 datefmt 参数，如下例所示:\n",
    "> datefmt 参数的格式与 time.strftime() 支持的格式相同。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f7f090b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[06/02/2025 04:32:20 PM] is when this event was logged.\n"
     ]
    }
   ],
   "source": [
    "# 运行前请重启py kernel\n",
    "import logging\n",
    "logging.basicConfig(format='%(asctime)s %(message)s', datefmt='[%m/%d/%Y %I:%M:%S %p]')\n",
    "logging.warning('is when this event was logged.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58c56fcf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
